home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_text / faqs / forthfaq / cas_ndcs < prev    next >
Encoding:
Internet Message Format  |  1993-06-28  |  5.6 KB

  1. Path: senator-bedfellow.mit.edu!enterpoop.mit.edu!news.media.mit.edu!uhog.mit.edu!wupost!howland.reston.ans.net!gatech!pitt!willett!ForthFAQ
  2. From: ForthFAQ@willett.pgh.pa.us (FAQ account for comp.lang.forth)
  3. Newsgroups: comp.lang.forth,comp.answers,news.answers
  4. Subject: Forth FAQ: CASE,OF,ENDOF,ENDCASE.  (l/m 30.Jan.93)
  5. Message-ID: <4601.UUL1.3#5129@willett.pgh.pa.us>
  6. Date: 23 Jun 93 11:11:16 GMT
  7. Expires: 07 Jul 93 23:59:59 EDT
  8. References: <4584.UUL1.3#5129@willett.pgh.pa.us>
  9. Followup-To: poster
  10. Lines: 134
  11. Approved: news-answers-request@MIT.Edu
  12. Xref: senator-bedfellow.mit.edu comp.lang.forth:12784 comp.answers:1108 news.answers:9690
  13.  
  14. Archive-name: ForthFaq/CASE_ENDCASE
  15. Last-modified: 30.Jan.93
  16. Version: 1.1
  17.  
  18.  
  19.  
  20.   [This is a message that I thought deserved to be preserved.  -dwp]
  21.  
  22.  
  23.  
  24.     From: eaker@ukulele.crd.ge.com (Chuck Eaker)
  25.     Subject: Re: Wanted .. CASE,OF,ENDOF,ENDCASE
  26.     Message-ID: <1992Nov25.164255.23225@crd.ge.com>
  27.     Date: 25 Nov 92 16:42:55 GMT
  28.  
  29.     In article <jax.722669998@well.sf.ca.us>,
  30.         jax@well.sf.ca.us (Jack J. Woehr) writes:
  31.     |> In <1992Nov24.101857.84@wronz.org.nz> mentink@wronz.org.nz writes:
  32.     |> 
  33.     |> 
  34.     |> 
  35.     |> >    Can anyone help with source ( masm/forth) to the CASE statement
  36.     |> >    word set. I.E .... CASE OF ENDOF ENDCASE ....
  37.     |> 
  38.     |>     Baden's CASE is in FORTH Dimensions VIII/5.
  39.     |> 
  40.     |>     Eaker, who wrote and enduring CASE construct, checks into
  41.     |> this newsgroup once and a while. Charles?
  42.     |> 
  43.     |>         =jax=
  44.     |> -- 
  45.     |>  # jax@well.{UUCP,sf.ca.us}  # #  Member  # # Vice President,       #
  46.     |>  # du!isis!koscej!jax        # # X3J14 TC # #  Forth Interest Group #
  47.     |>  # JAX on GEnie              # # for ANS  # #   P.O. Box 8231       #
  48.     |>  # SYSOP RCFB (303) 278-0364 # #  Forth   # #    San Jose CA 95155  #
  49.  
  50.     1. FIG-Forth
  51.     Here is the source for FIG-Forth published with the original article
  52.     (Forth Dimensions, Vol. II, No. 3, pp. 37-40.). The ?PAIRS word was
  53.     FIG-Forth's way of implementing a small amount of syntax checking.
  54.  
  55.      : CASE     ?COMP CSP @ !CSP 4 ; IMMEDIATE
  56.      : OF       4 ?PAIRS COMPILE OVER COMPILE = COMPILE OBRANCH
  57.             HERE 0 ,   COMPILE DROP  5 ; IMMEDIATE
  58.      : ENDOF    5 ?PAIRS COMPILE BRANCH HERE 0 ,
  59.             SWAP 2 [COMPILE] ENDIF 4 ; IMMEDIATE
  60.      : ENDCASE  4 ?PAIRS COMPILE DROP
  61.             BEGIN SP@ CSP @ = 0=
  62.               WHILE 2 [COMPILE ENDIF REPEAT
  63.             CSP ! ; IMMEDIATE
  64.  
  65.     1a. Here is additional source for FIG-Forth published in Forth
  66.     Dimensions, Vol. III, No. 6, pp. 187-188 in an article by Alfred J.
  67.     Monroe.  He adds a primitive compiled by OF which reduces the amount
  68.     of code compiled by OF. Use the definitions of CASE, ENDOF, and
  69.     ENDCASE given above.
  70.  
  71.      : (OF)     OVER = IF DROP 1 ELSE 0 ENDIF ;
  72.      : OF       4 ?PAIRS COMPILE (OF) COMPILE 0BRANCH
  73.             HERE 0 , 5 ; IMMEDIATE
  74.  
  75.     Mr. Monroe also gave code for some interesting variants:
  76.  
  77.      : (<OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  78.      : <OF      4 ?PAIRS COMPILE (<OF) COMPILE 0BRANCH
  79.             HERE 0 , 5 ; IMMEDIATE
  80.      : (>OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  81.      : >OF      4 ?PAIRS COMPILE (>OF) COMPILE 0BRANCH
  82.             HERE 0 , 5 ; IMMEDIATE
  83.      : RANGE    >R OVER DUP R> 1+ < IF SWAP 1- > IF DROP 1 ELSE 0
  84.             ENDIF ELSE DROP DROP 0 ENDIF ;
  85.      : RNG-OF   4 ?PAIRS COMPILE RANGE COMPILE 0BRANCH
  86.             HERE 0 , 5 ; IMMEDIATE
  87.  
  88.     1b. It is quite common to define (OF) as a CODE word and have
  89.     it combine the functions of the run-time (OF) and the compile-time
  90.     0BRANCH in the previous definitions. This reduces the amount of
  91.     compiled code even more.
  92.      CODE (OF) ( 1. Remove the top element of the stack and call it A.
  93.              2. If A equals the new top element of the stack,
  94.                 remove the new top element of the stack,
  95.                 skip over the branch vector, and execute
  96.                 the code which follows it.
  97.             Else
  98.                 continue execution at the location indicated
  99.                 by the branch vector.
  100.             ) END-CODE
  101.      : OF      4 ?PAIRS COMPILE (OF) HERE 0 , 5 ; IMMEDIATE
  102.  
  103.     2. dpANS-3
  104.     dpANS-3 contains the following definitions (p. 133) to illustrate
  105.     control structure extension. Note that it would be quite easy to
  106.     optimize OF along the lines suggested above. Note also that there is no
  107.     syntax checking.  These words may appear anywhere and not necessarily
  108.     combined with each other. In fact, ENDOF may be dispensed with entirely
  109.     and replaced with ELSE. Compile-time monitoring of the syntax of
  110.     control structure words is a perennial Forth problem.
  111.  
  112.      0 CONSTANT CASE IMMEDIATE  ( init count of OFs )
  113.  
  114.      : OF  ( #of -- orig #of+1 / x -- )
  115.         1+    ( count OFs )
  116.         >R    ( move off the stack in case the control-flow )
  117.           ( stack is the data stack. )
  118.         POSTPONE OVER  POSTPONE = ( copy and test case value )
  119.         POSTPONE IF    ( add orig to control flow stack )
  120.         POSTPONE DROP  ( discards case value if = )
  121.         R> ;           ( we can bring count back now )
  122.      IMMEDIATE
  123.  
  124.      : ENDOF  ( orig1 #of -- orig2 #of )
  125.         >R    ( move off the stack in case the control-flow )
  126.           ( stack is the data stack. )
  127.         POSTPONE ELSE
  128.         R> ;  ( we can bring count back now )
  129.      IMMEDIATE
  130.  
  131.      : ENDCASE ( orig 1..orign #of -- )
  132.         POSTPONE DROP  ( discard case value )
  133.         0 ?DO
  134.           POSTPONE THEN
  135.         LOOP ;
  136.      IMMEDIATE
  137.  
  138.     -- 
  139.     Chuck Eaker / P.O. Box 8, K-1 3C12 / Schenectady, NY 12301 USA
  140.     eaker@crd.ge.com        eaker@crdgw1.UUCP       (518) 387-5964
  141. ---
  142. If you have any questions about ForthNet/comp.lang.forth or any information
  143. to add/delete or correct in this message or any suggestions on formatting or
  144. presentation, please contact Doug Philips at one of the following addresses:
  145.           Internet: dwp@willett.pgh.pa.us
  146.           Usenet:   ...!uunet!willett.pgh.pa.us!dwp
  147.           GEnie:    D.PHILIPS3
  148.